 package com.ecb.business.utils;

 import com.alibaba.fastjson.JSON;
 import com.ecb.business.exception.BusinessException;
 import lombok.experimental.UtilityClass;
 import lombok.extern.slf4j.Slf4j;

 import javax.servlet.http.HttpServletRequest;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.util.Map;


 @Slf4j
 @UtilityClass
 public class RequestUtil {

     public static final String AUTH = "token";

     public String getTokenByRequest(HttpServletRequest request) {

         String headerValue = request.getHeader(AUTH);
         if (headerValue == null) {
             throw new BusinessException("401", "token为空");
         }
         String token = null;
         try {
             token = URLDecoder.decode(headerValue, "utf-8");
         } catch (UnsupportedEncodingException e) {
             log.error(e.getMessage(), e);
         }
         return token;
     }
     public Map<String, Object> getBodyByRequest(HttpServletRequest request) throws IOException {
         BufferedReader reader = request.getReader();
         StringBuilder requestBody = new StringBuilder();
         String line;
         while ((line = reader.readLine()) != null) {
             requestBody.append(line);
         }
         String requestBodyString = requestBody.toString();
         return JSON.parseObject(requestBodyString);
     }
 }
